import APIURL from '../../APIURL';

const state = {
  uid: localStorage.getItem('mp-uid') || '',
  nickname: localStorage.getItem('mp-nickname') || '',
  token: localStorage.getItem('mp-token') || '',
  status: JSON.parse(localStorage.getItem('mp-status')) || false,
  verified: false,
};

const mutations = {
  _login(state, payload) {
    state.uid = payload.uid;
    state.token = payload.token;
    state.status = true;
    state.verified = true;
    localStorage.setItem('mp-uid', state.uid);
    localStorage.setItem('mp-token', state.token);
    localStorage.setItem('mp-status', state.status);
  },
  _signup(state, payload) {
    state.uid = payload.uid;
    state.token = payload.token;
    state.nickname = payload.nickname;
    state.status = true;
    state.verified = true;
    localStorage.setItem('mp-uid', state.uid);
    localStorage.setItem('mp-token', state.token);
    localStorage.setItem('mp-nickname', state.nickname);
    localStorage.setItem('mp-status', state.status);
  },
  _logout(state) {
    state.uid = '';
    state.token = '';
    state.nickname = '';
    state.status = false;
    state.verified = false;
    localStorage.removeItem('mp-uid');
    localStorage.removeItem('mp-token');
    localStorage.removeItem('mp-nickname');
    localStorage.removeItem('mp-status');
  },
  _setNickname(state, nickname) {
    state.nickname = nickname;
    localStorage.setItem('mp-nickname', nickname);
  },
  _verify(state) {
    state.verified = true;
  },
};

const actions = {
  login({
    commit,
    dispatch,
  }, payload) {
    return new Promise((resolve, reject) => {
      APIURL.LOGIN(payload.uid, payload.password)
        .then((res) => {
          const data = res.data;
          if (data.success) {
            const token = data.token;
            commit('_login', {
              uid: payload.uid,
              token,
            });
            // resolve();
          } else {
            reject('账号或密码错误');
          }
        })
        .catch(() => {
          reject('服务器连接失败');
        })
        .then(() => {
          dispatch('getNickname')
            .then(() => {
              resolve();
            })
            .catch((msg) => {
              reject(msg);
            });
        });
    });
  },
  signup({
    commit,
  }, payload) {
    return new Promise((resolve, reject) => {
      APIURL.SIGNUP(payload.uid, payload.nickname, payload.password)
        .then((res) => {
          const data = res.data;
          if (data.success) {
            const token = data.token;
            commit('_signup', {
              uid: payload.uid,
              token,
              nickname: payload.nickname,
            });
            resolve();
          } else {
            reject('注册失败');
          }
        })
        .catch(() => {
          reject('服务器连接失败');
        });
    });
  },
  logout({
    commit,
  }) {
    commit('_logout');
  },
  getNickname({
    commit,
    state,
  }) {
    if (!state.status) {
      return Promise.reject();
    }
    return new Promise((resolve, reject) => {
      APIURL.GETNICKNAME(state.token)
        .then((res) => {
          const data = res.data;
          if (data.success) {
            const nickname = data.data.nickname;
            commit('_setNickname', nickname);
            resolve();
          } else {
            reject('登录过期');
          }
        })
        .catch(() => {
          reject('服务器连接失败');
        });
    });
  },
  verify({
    commit,
    state,
    dispatch,
  }) {
    if (state.verified) {
      return Promise.resolve();
    }
    return new Promise((resolve, reject) => {
      dispatch('getNickname')
        .then(() => {
          commit('_verify');
          resolve();
        })
        .catch((msg) => {
          if (msg === '登录过期') {
            commit('_logout');
          }
          reject(msg);
        });
    });
  },
};

export default {
  state,
  mutations,
  actions,
};
